﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QWeiBo.ViewModel;
using System.Collections.ObjectModel;
using TencentWeiboSDK.Model;
using TencentWeiboSDK.Services;
using System.Threading;
using TencentWeiboSDK.Services.Util;

namespace QWeiBoN.ViewModule
{
    public class MainMyFansModel : BaseViewModel
    {
        /// <summary>
        /// 实例化朋友服务类
        /// </summary>
        private FriendsService friendService = new FriendsService();

        /// <summary>
        /// 构造方法
        /// </summary>
        public MainMyFansModel()
        {
            MyFans = new ObservableCollection<User>();
        }

        /// <summary>
        /// 刷新我的fans需要的数据
        /// </summary>
        /// <param name="action"></param>
        public void RefreshMyFans(Action action)
        {
            // 异步刷新可以让用户有更好的操作体验
            new Thread(() =>
            {
                //获得我的fans列表
                friendService.Fanlist(new ServiceArgument() { Reqnum = 30 }, (callBackFans) =>
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        // 清空原始数据
                        MyFans.Clear();
                        callBackFans.Data.ForEach(a =>
                        {
                            // 对数返回据进行处理
                            if (a.Isidol == 0)
                            {
                                a.Email = "收听";
                            }
                            else
                            {
                                a.Email = "取消";
                            }
                            if (null == a.Head || a.Head.Equals(""))
                            {
                                a.Head = "Resources/head_50.jpg";
                            }
                            MyFans.Add(a);
                        });

                        // 切换并回调到UI线程.
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            if (null != action)
                            {
                                action();
                            }
                        });
                    });
                });
            }).Start();
        }

        /// <summary>
        /// 获取或设置首页时间线
        /// </summary>
        public ObservableCollection<User> MyFans
        {
            get;
            set;
        }
    }
}
